Ext.define('SRMT.controller.Register', {
	extend: 'Ext.app.Controller',
	
	views: [
	        'Register',
	        'Viewport'
	],
	
	refs: [{
		ref: 'registerForm',
		selector: 'srmtRegister'
	},{
		ref:'edtUserName',
		selector: 'srmtRegister > #edtUserName'
	},{
		ref: 'edtEmail',
		selector: '#edtEmail'
	},{
		ref: 'edtUserID',
		selector: '#edtUserID'
	},{
		ref: 'edtPassword',
		selector: '#edtPassword'
	},{
		ref: 'btnReset',
		selector: '#btnReset'
	},{
		ref: 'viewport',
		selector: 'viewport'
	}],
	
	init: function() {
		this.control({
			'srmtRegister': {
				render: this.onRender
			},
						
			'srmtRegister #btnReset': {
				click: this.resetForm
			},
			
			'srmtRegister #btnSubmit': {
				click: this.submitForm
			}
		});
	},
	
	onRender: function() {
		
	},
	
	resetForm: function() {
		this.getRegisterForm().getForm().reset();
	},
	
	submitForm: function() {
		var form = this.getRegisterForm().getForm();
		var viewport = this.getViewport();
		var winRegisterForm = this.getRegisterForm().up('window');
		viewport.disable();
		if(form.isValid()) {
			form.submit({
				url: 'php/module/authenticate/register.php',
				method: 'POST',
//				params: {
//				         userid: userid,
//				         password: password
//				},
				async: false,
				waitMsg: 'Registering ...',
				timeout: 5000,
				success: function(form, action) {
					Ext.MessageBox.show({
						title: 'Register Success',
						msg: 'Hello <b>'+action.result.user.username+'</b>!'+
							'<br>You\re now register with:<br>'+
							'User ID = <b>'+action.result.user.userid+'</b>',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.INFO
						});
					winRegisterForm.destroy();
                },
				failure: function(form, action) {
					Ext.MessageBox.show({
						title: 'Register Failed',
						msg: 'Please try again later!',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.ERROR
						});
				}
			});
			
		}
		viewport.enable();
		
	}
});